package com.users.rn.rncommon.model;

import android.os.Bundle;
import android.text.TextUtils;

import com.users.rn.rncommon.util.ReactConst;
import com.users.rn.rncommon.util.ReactUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 11:07
 */
public class SchemeModule implements Serializable {
    private String pluginid;
    private String moduleid;
    private String componentname;
    private Bundle params;
    private String enable;
    private String launchmode;
    private String type;

    private SchemeModule(String pluginid, String moduleid, String componentname, Bundle params, String enable, String launchmode, String type) {
        this.pluginid = pluginid;
        this.moduleid = moduleid;
        this.componentname = componentname;
        this.params = params;
        this.enable = enable;
        this.launchmode = launchmode;
        this.type = type;
    }

    private SchemeModule() {
    }

    public static SchemeModule deserialize(String json) {
        try {
            if (json != null && !json.isEmpty()) {
                return deserialize(new JSONObject(json));
            }
            return null;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static SchemeModule deserialize(JSONObject json) {
        if (json != null && json != JSONObject.NULL && json.length() > 0) {
            SchemeModule result = new SchemeModule();

            if (!json.isNull(ReactConst.PLUGINID)) {
                result.pluginid = json.optString(ReactConst.PLUGINID);
            }
            if (!json.isNull(ReactConst.MODULEID)) {
                result.moduleid = json.optString(ReactConst.MODULEID);
            }
            if (!json.isNull(ReactConst.COMPONENTNAME)) {
                result.componentname = json.optString(ReactConst.COMPONENTNAME);
            }
            String type = null;
            if (!json.isNull(ReactConst.TYPE)) {
                type = json.optString(ReactConst.TYPE);
                result.type = type;
            }
            if (TextUtils.isEmpty(type)) {
                if (!json.isNull(ReactConst.PARAMS)) {
                    String params = json.optString(ReactConst.PARAMS);
                    result.params = ReactUtils.jsonStringToBundle(params);
                }
            } else if (type.equalsIgnoreCase("1")) {
                if (!json.isNull(ReactConst.PARAMS)) {
                    String params = json.optString(ReactConst.PARAMS);
                    result.params = ReactUtils.jsonStringToBundleNew(params);
                }
            }
            if (!json.isNull(ReactConst.ENABLE)) {
                result.enable = json.optString(ReactConst.ENABLE);
            }
            if (!json.isNull(ReactConst.LAUNCHMODE)) {
                result.launchmode = json.optString(ReactConst.LAUNCHMODE);
            }
            return result;
        }
        return null;
    }

    public int getAnimationType() {
        int animationType = 0;
        if (params != null) {
            try {
                animationType = params.getInt(ReactConst.ANIMATION_TYPE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return animationType;
    }

    /**
     * 对于config文件中配置的参考开关enable
     *
     * @return
     */
    public boolean isModelValidForConfig() {
        return !TextUtils.isEmpty(pluginid)
                && !TextUtils.isEmpty(moduleid)
                && !TextUtils.isEmpty(componentname)
                && !TextUtils.isEmpty(enable)
                && enable.equalsIgnoreCase("1");
    }

    public boolean isModelValid() {
        return !TextUtils.isEmpty(pluginid)
                && !TextUtils.isEmpty(moduleid)
                && !TextUtils.isEmpty(componentname);
    }


    @Override
    public String toString() {
        return "RnSchemeModel{" +
                "pluginId='" + pluginid + '\'' +
                ", moduleId='" + moduleid + '\'' +
                ", componentName='" + componentname + '\'' +
                ", params=" + params + '\'' +
                ", enable=" + enable + '\'' +
                ", launchmode=" + launchmode + '\'' +
                ", type=" + type + '\'' +
                '}';
    }

    public String getPluginid() {
        return pluginid;
    }

    public String getModuleid() {
        return moduleid;
    }

    public String getComponentname() {
        return componentname;
    }

    public Bundle getParams() {
        return params;
    }

    public String getEnable() {
        return enable;
    }

    public String getLaunchmode() {
        return launchmode;
    }

    public String getType() {
        return type;
    }


    public static class RNSchemeBuilder {
        private String pluginid;
        private String moduleid;
        private String componentname;
        private Bundle params;
        private String enable;
        private String launchmode;
        //1就是解析jsonObject,其他值是按照线上解析方法解析，二层及其以上的参数会被转成jsonString
        private String type;


        public RNSchemeBuilder pluginId(String pluginId) {
            this.pluginid = pluginId;
            return this;
        }

        public RNSchemeBuilder moduleId(String moduleId) {
            this.moduleid = moduleId;
            return this;
        }

        public RNSchemeBuilder componentName(String componentName) {
            this.componentname = componentName;
            return this;
        }

        public RNSchemeBuilder params(Bundle params) {
            this.params = params;
            return this;
        }

        public RNSchemeBuilder enable(String enable) {
            this.enable = enable;
            return this;
        }

        public RNSchemeBuilder launchmode(String launchmode) {
            this.launchmode = launchmode;
            return this;
        }

        public RNSchemeBuilder type(String type) {
            this.launchmode = type;
            return this;
        }

        public SchemeModule build() {
            return new SchemeModule(pluginid, moduleid, componentname, params, enable, launchmode, type);
        }

    }
}
